package com.leetcode.LC;

public class LC892 {

    public int surfaceArea(int[][] grid) {//正视图面积每行最大值，侧视图面积每列最大值，俯视图面积方块个数--》这种办法行不通因为存在甜甜圈形状堆
        int a = 0, b = 0, c = 0, max;
        for (int[] ints : grid) {
            max = -1;
            for (int anInt : ints) {
                if (anInt!=0){
                    c++;
                }
                max = Math.max(anInt, max);
            }
            a += max;
        }

        for (int i = 0; i < grid[0].length; i++) {
            max = grid[0][i];
            for (int j = 0; j < grid.length; j++) {
                max = Math.max(max, grid[j][i]);
            }
            b += max;
        }
        System.out.println(a+","+b+","+c);

        return (a + b + c)*2;
    }
    public int surfaceArea2(int[][] grid) {
        //https://leetcode-cn.com/problems/surface-area-of-3d-shapes/solution/hua-tu-ji-suan-san-ge-zhong-die-bu-fen-by-liweiwei/
        // 习惯上应该做参数检查，但题目中给出了 N >= 1 ，故可以略去
        int rows = grid.length;
        // 题目保证了输入一定是 N * N，但为了使得程序适用性更强，还是单独把 cols 做赋值
        int cols = grid[0].length;

        int sum = 0;
        // 垂直重叠
        int verticalOverlap = 0;
        // 行重叠
        int rowOverlap = 0;
        // 列重叠
        int colOverlap = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                sum += grid[i][j];

                if (grid[i][j] > 1) {
                    verticalOverlap += (grid[i][j] - 1);
                }

                if (j > 0) {
                    rowOverlap += Math.min(grid[i][j - 1], grid[i][j]);
                }

                if (i > 0) {
                    colOverlap += Math.min(grid[i - 1][j], grid[i][j]);
                }
            }
        }
        return sum * 6 - (verticalOverlap + rowOverlap + colOverlap) * 2;

/*      作者：liweiwei1419
        链接：https://leetcode-cn.com/problems/surface-area-of-3d-shapes/solution/hua-tu-ji-suan-san-ge-zhong-die-bu-fen-by-liweiwei/
        来源：力扣（LeetCode）
        著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。*/
    }

}
